#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:do_flask_post.py
# author:张世航
# datetime:2024/12/3 19:10
# software: PyCharm
'''
    POST请求方式接口，带有参数；通过Body方式传参(form-data格式)
'''
from flask import Flask, request
import json

app = Flask(__name__)

@app.route('/api', methods=['POST'])  # 定义路由，并指明其请求方式为POST
def testPost():
    # 定义返回的数据的默认值
    return_dict = {'code': '200', 'message': 'successful!', 'data': None}

    # 判断入参是否为空，参数位置为Body,form-data格式
    if len(request.form) == 0:
        return_dict['code'] = '5004'  # 自定义状态码
        return_dict['message'] = '请求参数为空'
        return json.dumps(return_dict, ensure_ascii=False)

    name = request.form.get('name')
    age = request.form.get('age')

    # 对参数进行操作和处理，并构造返回数据
    return_dict['data'] = {
        'name': name,
        'age': age
    }
    return json.dumps(return_dict,ensure_ascii=False)

if __name__ == '__main__':
    app.run(port=5000, debug=True)
